package CtrLayer;

import java.sql.SQLException;

import DBLayer.*;
import ModelLayer.*;

public class CustomerCtr {
	
	private CustomerDB customerDb;

	/**
	 * Opret en Person
	 * 
	 * @param name
	 * @param address
	 * @param zipcode
	 * @param City
	 * @param phoneNo
	 */
	public void addCustomer(String name, String address, int zipcode, String phoneNo) throws InvalidZipcodeException
	{
		// Hent by navn fra db via post nr.
		CityDB cityDB = new CityDB();
		String city = cityDB.getCity(zipcode);
		
		if (city == null)
			throw new InvalidZipcodeException();
		
		// Lav customer.
		Customer c = new Customer(name, address, zipcode, city, phoneNo);
		
		// TilfÃ¸j customer til db.
		CustomerDB customerdb = new CustomerDB();
		try
		{
			ConnectionDB.startTransaction();
			customerdb.createCustomer(c);
			ConnectionDB.commitTransaction();
		}
		catch (SQLException e)
		{
			ConnectionDB.rollbackTransaction();
		}
	}

	public Customer findCustomer(String phoneno)
	{
		customerDb = new CustomerDB();
		return customerDb.findCustomer(phoneno);
	}
	
	/**
	 * Thrown on an invalid zipcode.
	 * @author Bruno
	 *
	 */
	@SuppressWarnings("serial")
	public class InvalidZipcodeException extends Exception { }
}
